﻿using System;
using System.Configuration;
using System.IO;
using System.Linq;
using Ipmce.RemoteVoting.Backend.DataAccess;

namespace Ipmce.RemoteVoting.Backend.SortingApplication
{
	class Program
	{
		static void Main(string[] args)
		{
			if (args.Length != 1)
			{
				Console.WriteLine("Usage: dump folder");
                return;
			}
			var dumpFolder = args[0];
			if (!Directory.Exists(dumpFolder))
			{
				Directory.CreateDirectory(dumpFolder);
			}

			using (var context = new RemoteVotingContext(ConfigurationManager.AppSettings["RemoteVotingDataBase"]))
			{
				var repository = new VoterRepository(context);

				var voters = repository.GetAllVoters();

				foreach (var voter in voters.Where(voter => voter.Vote != null))
				{
					File.WriteAllBytes(Path.Combine(dumpFolder,Guid.NewGuid().ToString()), voter.Vote);
					voter.Vote = null;
				}

				repository.CommitChanges();
			}
		}
	}
}
